-- EXEC sp_update_chiptrac_vendor_Number
ALTER PROC sp_update_chiptrac_vendor_Number
AS
BEGIN
	DECLARE @vendor_id INT
	DECLARE @vendorNumber VARCHAR(15)

	DECLARE @vendorCursor CURSOR
	SET @vendorCursor = CURSOR FOR SELECT CT_VENDOR_ID, GP_VENDOR_NUMBER FROM CT_GP_Vendor_Mapping	
	
	OPEN @vendorCursor
	FETCH NEXT FROM @vendorCursor INTO @vendor_id, @vendorNumber
	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF @vendorNumber IS NOT NULL AND LEN( LTRIM( RTRIM( @vendorNumber ) ) ) > 0
		BEGIN
			UPDATE VENDOR SET V_Number = @vendorNumber WHERE VENDOR_ID = @vendor_id
		END
		ELSE
		BEGIN
			UPDATE VENDOR SET V_Number = SUBSTRING( LTRIM( RTRIM( V_Name ) ), 1, 15 ) WHERE V_Number IS NULL OR LEN( LTRIM( RTRIM( V_Number) ) ) < 1 AND VENDOR_ID = @vendor_id
		END

	FETCH NEXT FROM @vendorCursor INTO @vendor_id, @vendorNumber
	END
	CLOSE @vendorCursor
	DEALLOCATE @vendorCursor
	
	
END